/**
 * 路由函数
 * @param {String} mode 路由模式 默认hash
 * @param {Array} routes 路由列表
 */
function createRouter(mode = "hash", routes, viewRouter) {
  // 定义路由表   
  const routeMap = routes.reduce((prev, route) => prev.set(route.path, route.component), new Map())
  // 根据不同的mode选择不同的路由方式  
  if (mode === "hash") {    // hash模式    
    window.addEventListener("hashchange", () => {
      const path = location.hash.slice(1);
      const component = routeMap.get(path) ?? routeMap.get('*');
      viewRouter.innerHTML = component();
    });
    // 初始化
    const path = location.hash.slice(1);
    const component = routeMap.get(path) ?? routeMap.get('*');
    viewRouter.innerHTML = component();
  } else if (mode === "history") {    // history模式 
    window.addEventListener("popstate", () => {
      const component = routeMap.get(history.state?.path) ?? routeMap.get('/home');
      viewRouter.innerHTML = component();
    });
    // 初始化 默认首页
    const component = routeMap.get('/home');
    viewRouter.innerHTML = component();
    ;[...viewRouter.nextElementSibling.children].forEach(ele => ele.onclick = () => {
      const component = routeMap.get(ele.getAttribute('path')) ?? routeMap.get('*');
      viewRouter.innerHTML = component();
      history.pushState({ path: ele.getAttribute('path') }, "");
    })
  }
}


;(function () {
  let myGlobal;


  //将插件暴露给全局对象
  myGlobal = (function(){return this || (0,eval)('this')}());
    
  if(typeof module !== 'undefined' && module.exports){
      module.exports = AdkDate;
  }else if (typeof define === "function" && define.amd){
      define(function () {
          return AdkDate;
      })
  }else {
      !('AdkDate' in myGlobal) && (myGlobal.AdkDate = AdkDate);
  }
})()